package cn.edu.gdgm.system.utils.mqtt.client;

import cn.edu.gdgm.system.config.MqttPropertiesConfig;
import cn.edu.gdgm.system.enums.QosEnum;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.paho.client.mqttv3.*;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.annotation.Resource;
import java.nio.charset.StandardCharsets;

/**
 * @author quan
 * @date 2021-03-07
 */
@Component
@Slf4j
public class EmqClient {

    private IMqttClient mqttClient;

    @Resource
    private MqttPropertiesConfig mqttPropertiesConfig;

    @Resource
    private MessageCallback mqttCallback;

    @PostConstruct
    public void init() {
        // 持久性存储在内存
        MemoryPersistence memoryPersistence = new MemoryPersistence();

        try {
            mqttClient = new MqttClient(mqttPropertiesConfig.getBrokerUrl(), mqttPropertiesConfig.getClientId(), memoryPersistence);
        } catch (MqttException e) {
            e.printStackTrace();
        }

    }

    public void connect() {
        MqttConnectOptions mqttConnectOptions = new MqttConnectOptions();
        mqttConnectOptions.setAutomaticReconnect(true);
        log.info("username:{}, pwd:{}", mqttPropertiesConfig.getUsername(), mqttPropertiesConfig.getPassword());
        mqttConnectOptions.setUserName(mqttPropertiesConfig.getUsername());
        mqttConnectOptions.setPassword(mqttPropertiesConfig.getPassword().toCharArray());
        mqttConnectOptions.setPassword("java".toCharArray());
        mqttConnectOptions.setCleanSession(true);

        mqttClient.setCallback(mqttCallback);
        try {
            mqttClient.connect(mqttConnectOptions);
        } catch (MqttException e) {
            e.printStackTrace();
        }
    }

    @PreDestroy
    public void disconnect() {
        try {
            mqttClient.disconnect();
        } catch (MqttException e) {
            e.printStackTrace();
        }
    }

    public void reconnect() {
        try {
            mqttClient.reconnect();
        } catch (MqttException e) {
            e.printStackTrace();
        }
    }

    public void publish(String topic, String msg, QosEnum qos, boolean retain) {
        MqttMessage mqttMessage = new MqttMessage();
        mqttMessage.setPayload(msg.getBytes(StandardCharsets.UTF_8));
        mqttMessage.setQos(qos.getValue());
        mqttMessage.setRetained(retain);

        try {
            mqttClient.publish(topic, mqttMessage);
        } catch (MqttException e) {
            e.printStackTrace();
        }
    }

    public void subscribe(String topicFilter, QosEnum qos) {
        try {
            mqttClient.subscribe(topicFilter, qos.getValue());
        } catch (MqttException e) {
            e.printStackTrace();
        }
    }

    public void unsubscribe(String topicFilter) {
        try {
            mqttClient.unsubscribe(topicFilter);
        } catch (MqttException e) {
            e.printStackTrace();
        }
    }

}
